// Filename : cgiHandler.h
// Definition of class cgiHandler

#ifndef CGIHANDLER_H
#define CGIHANDLER_H

#include "dictionary.h"
#include <string>

static const int NUM_OF_ENV_VARS = 19;
static const int MAX_LENGTH = 1024;	
static const string ENV_VARS[ NUM_OF_ENV_VARS ] = {	
	"AUTH_TYPE",
	"CONTENT_LENGTH",
	"CONTENT_TYPE",
	"GATEWAY_INTERFACE",
	"HTTP_ACCEPT",
	"HTTP_USER_AGENT",
	"PATH_INFOR",
	"PATH_TRANSLATED",
	"QUERY_STRING",
	"REMOTE_ADDR",
	"REMOTE_HOST",
	"REMOTE_IDENT",
	"REMOTE_USER",
	"REQUEST_METHOD",
	"SCRIPT_NAME",
	"SERVER_NAME",
	"SERVER_PORT",
	"SERVER_PROTOCOL",
	"SERVER_SOFTWARE"
};

#define H2D(x) (toupper(x)>='A'?toupper(x)-'A'+10:(x)-'0')

using namespace std;

// Make handle cgi resquset easy
class CgiHandler {
public:
	CgiHandler() {
		// nothing to do;
	}

	~CgiHandler() {
		// nothing to do;
	}

	void handlerInit();
	string getEnvValue( string ) const;
	string getQueryValue( string ) const;

protected:
	virtual bool doBeforeReadData( int );
	void loadEnvironmentVar();
	void loadQueryVar( string );

private:
	string getEnvWithCheck( string ) const;
	bool isNumber( string ) const;
	
	Dictionary environmentVar;	// Store enviroment variables
	Dictionary queryVar;		// Store query-string variables
};

#endif

// End of cgiHandler.h
